package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.model.common.dtos.common.ResponseResult;
import com.heima.model.common.dtos.wemedia.WmMaterialDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 自媒体端素材相关接口
 */
@Api(value = "/api/v1/material", tags = {"自媒体端素材相关接口"})
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {
    @Autowired
    private WmMaterialService wmMaterialService;

    /**
     * 素材上传功能
     * @param file
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "file", name = "file", value = "", required = true)
    })
    @ApiOperation(value = "素材上传功能", notes = "素材上传功能", httpMethod = "POST")
    @PostMapping("/upload_picture")
    public ResponseResult uploadPicture(@RequestParam("multipartFile") MultipartFile file){
        return wmMaterialService.uploadPicture(file);
    }

    /**
     * 素材列表显示功能
     * @param wmMaterialDto
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "WmMaterialDto", name = "wmMaterialDto", value = "", required = true)
    })
    @ApiOperation(value = "素材列表显示功能", notes = "素材列表显示功能", httpMethod = "POST")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmMaterialDto wmMaterialDto){
        return wmMaterialService.listMaterials(wmMaterialDto);
    }

    /**
     * 收藏功能
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "收藏功能", notes = "收藏功能", httpMethod = "GET")
    @GetMapping("/collect/{id}")
    public ResponseResult isCollect(@PathVariable("id")Integer id){
        LambdaUpdateWrapper<WmMaterial> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(WmMaterial::getId, id).set(WmMaterial::getIsCollection,1);
        wmMaterialService.update(wrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 取消收藏功能
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "取消收藏功能", notes = "取消收藏功能", httpMethod = "GET")
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult isCancelCollect(@PathVariable("id")Integer id){
        LambdaUpdateWrapper<WmMaterial> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(WmMaterial::getId, id).set(WmMaterial::getIsCollection,0);
        wmMaterialService.update(wrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除图片功能
     * @param id 素材id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "素材id", required = true)
    })
    @ApiOperation(value = "删除图片功能", notes = "删除图片功能", httpMethod = "GET")
    @GetMapping("/del_picture/{id}")
    public ResponseResult delPicture(@PathVariable("id")Integer id){
        return wmMaterialService.delPicture(id);

    }
}
